package main

import (
	"encoding/json"
	"io/ioutil"
	"os"

	"gitee.com/zh-jn/harmonica-previewer-online/api"
	"gitee.com/zh-jn/harmonica-previewer-online/compiler/deveco-compiler/v1/config"
	"gitee.com/zh-jn/harmonica-previewer-online/compiler/deveco-compiler/v1/service"
)

func CollectConfigFromEnv() config.Config {
	return config.Config{
		CompileEnvZip:      api.ParseString(os.Getenv("CompileService_CompileEnvZip"), "./CompileEnv.zip"),
		RunningHomeBase:    api.ParseString(os.Getenv("CompileService_RunningHomeBase"), `C:\\compile-service\`),
		AgentNum:           api.ParseIntValue(os.Getenv("CompileService_AgentNum"), 3),
		WorkloadsQueueSize: api.ParseIntValue(os.Getenv("CompileService_WorkloadsQueueSize"), 100),
		CompileTimeout:     api.ParseIntValue(os.Getenv("CompileService_CompileTimeout"), 30),

		Addr: api.ParseString(os.Getenv("LocalStorageService_Addr"), "0.0.0.0"),
		Port: api.ParseIntValue(os.Getenv("LocalStorageService_Port"), 8081),
	}
}

func CollectConfigFromFile(filename string) config.Config {
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		panic(err)
	}
	var cfg config.Config
	if err = json.Unmarshal(buf, &cfg); err != nil {
		panic(err)
	}
	return cfg
}

func main() {
	// cfg := CollectConfigFromEnv()
	cfg := CollectConfigFromFile("./config.json")
	service.RunServer(cfg)
}
